package com.jc.service.jgservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jc.common.util.Base64Util;
import com.jc.common.util.HttpUtil;
import com.jc.common.util.MD5;
import com.jc.framework.enums.Conclusion;
import com.jc.framework.enums.LogType;
import com.jc.service.model.jg.RequestBaseModel;
import com.jc.service.systemloglist.SystemLogListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@SuppressWarnings("all")
@Service
public class JgHttpServiceImpl implements JgHttpService{
	Logger log = LoggerFactory.getLogger(JgHttpServiceImpl.class);
    @Value("${jg.url}")
    private String url;
    @Value("${jg.secret}")
    private String appSecret;
    @Value("${jg.appkey}")
    private String appKey;
    @Autowired
    private SystemLogListService systemLogListService;


    @Override
    public JSONObject httpJgUtil(String method,Map<String,Object> paramMap,LogType logType){
        String result="";
        String URL = url + method;
        String dataJson = JSONObject.toJSONString(paramMap);
        String data = Base64Util.encode(dataJson);
        String sign = MD5.passmd5(data + appSecret).toUpperCase();
        RequestBaseModel requestBaseModel = new RequestBaseModel();
        requestBaseModel.setAppKey(appKey);
        requestBaseModel.setData(data);
        requestBaseModel.setSign(sign);
        String body = JSON.toJSONString(requestBaseModel);
        JSONObject returnObject = null;
        try {
            result = HttpUtil.postJson(URL, body, null);
            returnObject = JSONObject.parseObject(result);
            if(returnObject != null && returnObject.getInteger("code") == 10000) {
                systemLogListService.addSystemLog(body, Conclusion.OK, null, logType, result);
            }else {
                systemLogListService.addSystemLog(body, Conclusion.FAIL, null, logType, result);
            }
        } catch (Exception e) {
            log.error("请求吉贡失败: {},{}",e.getMessage(),e);
            systemLogListService.addSystemLog(body, Conclusion.FAIL, null, logType, result);
        }
        return returnObject;
    }
}
